package com.sf.guava.collect;

import com.google.common.collect.ContiguousSet;
import com.google.common.collect.DiscreteDomain;
import com.google.common.collect.Range;
import com.google.common.primitives.Ints;

/**
 * @Classname RangeTest
 * @Description 获取范围
 * @Date 2019/12/11 0011 16:07
 * @Created by 埔枘
 */
public class RangeTest {

    public static void main(String[] args) {
        checkConnect();
    }

    /**
     *  包含首尾的数据集合
     */
    public static void createCloseList(){
        //create a range [a,b] = { x | a <= x <= b}
        //创建一个 闭合的 数据集
        Range<Integer> range1 = Range.closed(0, 9);
        System.out.print("[0,9] : ");
        printRange(range1);
        //是否含有 5
        System.out.println("5 is present: " + range1.contains(5));
        //是否含有 1,2,3
        System.out.println("(1,2,3) is present: " + range1.containsAll(Ints.asList(1, 2, 3)));
        //获取最小值
        System.out.println("Lower Bound: " + range1.lowerEndpoint());
        //获取最大值
        System.out.println("Upper Bound: " + range1.upperEndpoint());
    }

    /**
     * 创建一个 open 的 数据集(不包含首尾)
      */
    public static void createOpenList(){
        Range<Integer> range2 = Range.open(0, 9);
        System.out.print("(0,9) : ");
        printRange(range2);
    }
    /**
     * 创建一个 open 的 数据集(不包含首 包含尾)
     */
    public static void createCloseOpenList(){
        //create a range (a,b] = { x | a < x <= b}
        Range<Integer> range3 = Range.openClosed(0, 9);
        System.out.print("(0,9] : ");
        printRange(range3);
    }
    /**
     * 创建一个 open 的 数据集(9 - 无穷大)
     */
    public static void createThan9List(){
        //create an open ended range (9, infinity
        Range<Integer> range5 = Range.greaterThan(9);
        System.out.println("(9,infinity) : ");
        System.out.println("Lower Bound: " + range5.lowerEndpoint());
        System.out.println("Upper Bound present: " + range5.hasUpperBound());
    }
    /**
     * 检查 检查[0,9]中的一个子区间[3,5]
     */
    public static void check(){
        //创建一个 闭合的 数据集
        Range<Integer> range1 = Range.closed(0, 9);

        Range<Integer> range6 = Range.closed(3, 5);
        printRange(range6);

        //check a subrange [3,5] in [0,9]
        System.out.println("[0,9] encloses [3,5]:" + range1.encloses(range6));
    }

    /**
     *  检查
     */
    public static void checkConnect(){
        Range<Integer> range1 = Range.closed(0, 9);
        Range<Integer> range7 = Range.closed(10, 20);
        printRange(range7);
        //check ranges to be connected
        //检查 是否有交集
        System.out.println("[0,9] is connected [9,20]:" + range1.isConnected(range7));

        Range<Integer> range8 = Range.closed(5, 15);

        // 获取交集
        printRange(range1.intersection(range8));
        // 获取并集
        printRange(range1.span(range8));


    }

    //打印
    private static void printRange(Range<Integer> range){
        System.out.print("[ ");
        for(int grade : ContiguousSet.create(range, DiscreteDomain.integers())) {
            System.out.print(grade +" ");
        }
        System.out.println("]");
    }


}
